package com.starbaba.core.web.struts2;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.Action;
import com.starbaba.core.service.CommonService;

/**
 * 使用Ajax请求的业务逻辑处理模板
 * 
 * @param <T>
 *
 * @Copyright: Copyright (c) 2006
 * @Company: Revenco Tech Ltd.
 * @anthor chenminhui<chenminhui@revenco.com>
 * @since 2014-11-12 上午11:27:59
 * @version V1.0
 */
public abstract class AjaxActionTemplate<T> {
	
	protected Logger log = LoggerFactory.getLogger(getClass());
	protected String busiName;	//业务名称
	
	public AjaxActionTemplate(String busiName) {
		this.busiName = busiName;
	}
	
	/**
	 * 设置业务逻辑
	 * 
	 * @param action
	 * @return
	 *
	 * @Copyright: Copyright (c) 2006
	 * @Company: Revenco Tech Ltd.
	 * @anthor chenminhui<chenminhui@revenco.com>
	 * @since 2014-11-12 上午09:41:17
	 * @version V1.0
	 */
	protected abstract T process(CommonService service) throws Exception;
	
	/**
	 * 真正执行业务逻辑
	 * 
	 * @return ExtJsonResult<T> 反馈
	 *
	 * @Copyright: Copyright (c) 2006
	 * @Company: Revenco Tech Ltd.
	 * @anthor chenminhui<chenminhui@revenco.com>
	 * @since 2014-11-12 上午11:28:17
	 * @version V1.0
	 */
	public ExtJsonResult<T> doProcess(CommonService service) {
		String errmsg = null;
		ExtJsonResult<T> result = new ExtJsonResult<T>();
		
		try {
			T rtnData = process(service);
			if(rtnData != null) {
				result.setData(rtnData);
			}
		} catch(Exception e) {
			errmsg = busiName + "！失败原因：" + e.getMessage();
			log.error(errmsg, e);
		}
		
		if(errmsg == null) {
			result.setStatus(Action.SUCCESS);
			result.setMessage(busiName + "成功");
		} else {
			result.setStatus(Action.ERROR);
			result.setMessage(errmsg);
		}
		return result;
	}
}
